from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
from . import models


class UserAuth(BaseAuthentication):
    def authenticate(self, request):
        token = request.META.get("HTTP_TOKEN")
        obj = models.Token.objects.filter(key=token).first()
        if obj:
            return obj.user, token
        else:
            raise AuthenticationFailed("认证失败")
